package com.nuoniu.sibanyun.service.erp.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.erp.ErpProcessRouteDetail;
import com.nuoniu.sibanyun.entity.vo.ProcessRouteAndPriceDetailVo;
import com.nuoniu.sibanyun.mapper.erp.ErpProcessRouteDetailMapper;
import com.nuoniu.sibanyun.service.erp.IErpProcessRouteDetailService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 工艺明细 服务实现类
 * </p>
 *
 * @author 小啊刚
 * @since 2020-12-30
 */
@Service
public class ErpProcessRouteDetailServiceImpl extends ServiceImpl<ErpProcessRouteDetailMapper, ErpProcessRouteDetail> implements IErpProcessRouteDetailService {

    @Override
    public void deleteByPRId(Long prId) {
        baseMapper.deleteByPRId(prId);
    }

    @Override
    public ErpProcessRouteDetail getFirstOrderDetail(Integer prId) {
        QueryWrapper<ErpProcessRouteDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("work_flag",1);
        queryWrapper.eq("pr_id",prId);
        queryWrapper.last("LIMIT 1");
        ErpProcessRouteDetail processRouteDetail = getOne(queryWrapper);
        return processRouteDetail;
    }

    @Override
    public ErpProcessRouteDetail getNextOrderDetail(Integer prId, Integer currentOrder) {
        QueryWrapper<ErpProcessRouteDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("work_order",currentOrder + 1);
        queryWrapper.eq("pr_id",prId);
        queryWrapper.last("LIMIT 1");
        ErpProcessRouteDetail processRouteDetail = getOne(queryWrapper);
        return processRouteDetail;
    }

    @Override
    public List<ProcessRouteAndPriceDetailVo> listProcessRouteAndPriceDetailVo(Integer prId) {
        UserInfo userInfo = UserThreadLocal.get();
        return baseMapper.listProcessRouteAndPriceDetailVo(prId, userInfo.getCompanyId());
    }

    @Override
    public List<ProcessRouteAndPriceDetailVo> listProcessRouteAndPriceDetailVoByRouteDetailIds(List<Integer> routeDetailIds) {
        if(null == routeDetailIds || routeDetailIds.size() == 0) {
            throw new NuoNiuException("请选择工艺明细");
        }
        UserInfo userInfo = UserThreadLocal.get();
        String routeDetailIdsStr = StringUtils.join(routeDetailIds, ",");
        return  baseMapper.listProcessRouteAndPriceDetailVoByRouteDetailIds(routeDetailIdsStr,userInfo.getCompanyId());
    }



}
